<%@taglib prefix="itrust" uri="/WEB-INF/tags.tld"%>
<%@page errorPage="/auth/exceptionHandler.jsp"%>

<%@page import="edu.ncsu.csc.itrust.action.EditOfficeVisitAction"%>
<%@page import="edu.ncsu.csc.itrust.beans.OfficeVisitBean"%>
<%@page import="edu.ncsu.csc.itrust.beans.InstructionBean"%>

<%@include file="/global.jsp"%>

<%
pageTitle = "iTrust - Edit Instruction Information";
%>

<%@include file="/header.jsp"%>

<%
boolean makeChange = false;
if (request.getParameter("change") != null) {
	if (request.getParameter("change").equalsIgnoreCase("True")) {
		makeChange = true;		
	}
}

String ovIDString = request.getParameter("ovID");
String pidString = (String)session.getAttribute("pid");
long insID = Long.parseLong(request.getParameter("insID"));

EditOfficeVisitAction officeAction = new EditOfficeVisitAction(prodDAO, loggedInMID, pidString, ovIDString);
OfficeVisitBean ov = officeAction.getOfficeVisit();

InstructionBean instruct = null;
for (InstructionBean ibean : officeAction.getInstructionsForOfficeVisit() ) {
	if (ibean.getInstructID() == insID) {
		instruct = ibean;
		break;
	}
}
if (instruct == null) {%>
<h1>ERROR: Instruction not found.</h1>
<%}else{%>
<%if (!makeChange) { %>
<%// This portion prints a form to edit the prescription information. %>
<form
	action="editInstructions.jsp?insID=<%= StringEscapeUtils.escapeHtml("" + (insID)) %>&ovID=<%= StringEscapeUtils.escapeHtml("" + (ovIDString)) %>&change=True"
	method=post>
<table align=center border=1 class="fTable">
	<tr>
		<th colspan=5>Edit Patient Specific Instructions</th>
	</tr>
	<tr class="subHeader">
		<td align=center>Name</td>
		<td align=center>URL</td>
		<td align=center>Comments</td>
	</tr>
	<tr>
		<td align=center><textarea name="addInstName" rows="2" cols="20"><%= StringEscapeUtils.escapeHtml("" + (instruct.getDescriptName() )) %></textarea></td>
		<td align=center><textarea name="addInstUrl" rows="2" cols="20"><%= StringEscapeUtils.escapeHtml("" + (instruct.getURL() )) %></textarea></td>
		<td align=center><textarea name="addInstComment" rows="2"
			cols="20"><%= StringEscapeUtils.escapeHtml("" + (instruct.getComments() )) %></textarea></td>
	</tr>
</table>
<br />
<center><input type="submit" value="Change"> <input
	type="reset" value="Reset"></center>
</form>

<%} else {%>
<%// This portion prints out the recently edited prescription information and provides a link to the home page.
		try {
			instruct.setInstructID(Long.parseLong(request.getParameter("insID")));
			instruct.setDescriptName(request.getParameter("addInstName"));
			instruct.setURL(request.getParameter("addInstUrl"));
			instruct.setComments(request.getParameter("addInstComment"));
			officeAction.editInstruction(instruct);
			loggingAction.logEvent(TransactionType.PATIENT_INSTRUCTIONS_EDIT, loggedInMID, ov.getPatientID(), "Office Visit ID: " + ov.getID());
			
			response.sendRedirect("http://localhost:8080/iTrust/auth/hcp-uap/editOfficeVisit.jsp?ovID=" + (ov.getID()));
			
		} catch(Exception e) {
			%><center><b>The Instruction could not be edited due to
an internal error.<br />
Please Try again later.<br /><%= StringEscapeUtils.escapeHtml("" + (e.toString() )) %></b></center>
<%
		}
		
		%>
<%}%>
<%}%>
<%@include file="/footer.jsp"%>